package com.example.administrator.newsmvp;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.example.administrator.newsmvp.about.AboutFragment;
import com.example.administrator.newsmvp.image.ImageFragment;
import com.example.administrator.newsmvp.main.presenter.MainPresenter;
import com.example.administrator.newsmvp.news.widget.NewsFragment;
import com.example.administrator.newsmvp.weather.WeatherFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends AppCompatActivity {
    private MainPresenter mPresenter;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.appbar)
    AppBarLayout appbar;
    @InjectView(R.id.frame_content)
    FrameLayout frameContent;
    @InjectView(R.id.main_content)
    CoordinatorLayout mainContent;
    @InjectView(R.id.navigation_view)
    NavigationView navigationView;
    @InjectView(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    /**
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        //绑定toolbar
        setSupportActionBar(toolbar);
//        mToolbar.setNavigationIcon(R.mipmap.ic_launcher);
        //获取结合工具
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.drawer_open, R.string.drawer_close);

        toggle.syncState();

        drawerLayout.addDrawerListener(toggle);
        //对其进行监听
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            int itemId = item.getItemId();
            //匹配每个fragmentid
            mPresenter.switchFragment(itemId);
            navigationView.setCheckedItem(itemId);
            drawerLayout.closeDrawers();
            return false;
        }
    });
        toNewFragment();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void toNewFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new NewsFragment()).commit();
        toolbar.setTitle(R.string.navigation_news);
    }
    public void toImageFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new ImageFragment()).commit();
        toolbar.setTitle(R.string.navigation_images);
    }

    public void toWeatherFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new WeatherFragment()).commit();
        toolbar.setTitle(R.string.navigation_weather);
    }

    public void toAboutFragment() {
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_content, new AboutFragment()).commit();
        toolbar.setTitle(R.string.navigation_about);
    }
}
